//
//  HYCellForSearch.m
//  TTLestening
//
//  Created by dllo on 16/3/25.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "HYCellForSearch.h"
#import "HYModelForSearch.h"

#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@implementation HYCellForSearch

#pragma mark - init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
    
        [self createSubViews];
    }
    
    return self;
}

#pragma mark - createSubViews
- (void)createSubViews {
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.labelForMusicCount = [[UILabel alloc] init];
    self.labelForMusicCount.textAlignment = NSTextAlignmentCenter;
    self.labelForMusicCount.font = [UIFont systemFontOfSize:25];
    self.labelForMusicCount.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.labelForMusicCount];
    
    
    self.labelForMusicName = [[UILabel alloc] init];
    self.labelForMusicName.font=[UIFont systemFontOfSize:19];
    [self.contentView addSubview:self.labelForMusicName];
    
    
    self.labelForSinger = [[UILabel alloc] init];
    self.labelForSinger.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.labelForSinger];
    
    
    self.labelForLikeCount = [[UILabel alloc] init];
    self.labelForLikeCount.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.labelForLikeCount];
    
    
    self.imageViewForLike = [[UIImageView alloc] init];
    [self.imageViewForLike setImage:[UIImage imageNamed:@"xin"]];
    [self.contentView addSubview:self.imageViewForLike];
    
    
    self.buttonForOptions = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonForOptions setImage:[UIImage imageNamed:@"iconfont-you"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.buttonForOptions];
    
    [self.buttonForOptions addTarget:self action:@selector(OptionsAction) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.labelForMusicCount.frame = CGRectMake(0, 0, WIDTH / 4, HEIGHT);
    
    self.labelForMusicName.frame = CGRectMake(WIDTH / 4, HEIGHT / 9, WIDTH / 2, HEIGHT / 3);
        
    self.labelForSinger.frame = CGRectMake(WIDTH / 4, HEIGHT / 2, WIDTH / 3 / 2, HEIGHT / 3.5);
    
    self.imageViewForLike.frame = CGRectMake(WIDTH / 2.5, HEIGHT / 1.9, WIDTH / 7 / 2, HEIGHT / 4.5);
    
    self.labelForLikeCount.frame = CGRectMake(WIDTH / 2.1, HEIGHT / 2, WIDTH / 3 / 2, HEIGHT / 3.5);
    
    self.buttonForOptions.frame = CGRectMake(WIDTH / 1.2, HEIGHT / 4, WIDTH / 7 / 2, HEIGHT / 4.5);
}


- (void)passMoldel:(HYModelForSearch *)model :(NSInteger)number {
    
    self.labelForMusicCount.text = [NSString stringWithFormat:@"%ld",number + 1];
    self.labelForMusicName.text = model.name;
    self.labelForSinger.text = model.singerName;
    
    if(model.favorites >= 10000){
    
     self.labelForLikeCount.text = [NSString stringWithFormat:@"%.1f万", round(model.favorites)/ 10000];
    
    } else {
    
    self.labelForLikeCount.text = [NSString stringWithFormat:@"%ld", model.favorites];
    
    }
}


- (void)OptionsAction {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:self.labelForMusicName.text message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *alertLike = [UIAlertAction actionWithTitle:@"收藏" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSLog(@"已收藏");
    }];
    
    UIAlertAction *alertDown = [UIAlertAction actionWithTitle:@"下载" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSLog(@"已下载");
    }];
    
    
    UIAlertAction *alertShare = [UIAlertAction actionWithTitle:@"分享" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSLog(@"分享成功");
    }];
    
    UIAlertAction *alertNext = [UIAlertAction actionWithTitle:@"下一首" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSLog(@"下一首");
    }];
    
    
    UIAlertAction *alertCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"取消");
    }];
    
    [alert addAction:alertLike];
    [alert addAction:alertDown];
    [alert addAction:alertShare];
    [alert addAction:alertNext];
    [alert addAction:alertCancel];
    
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    
    UIViewController *vc = (UIViewController*)object;
    [vc presentViewController:alert animated:YES completion:nil];
}















- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
